<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.math.BigDecimal" %>
<%--获取流水号--%>
<%
    JSONObject jsonData = new JSONObject();
    String lshlb = Util.null2String( request.getParameter( "lshlb" ) );
    String cghtlx = Util.null2String( request.getParameter( "cghtlx" ) );
    RecordSet rs = new RecordSet();
    String cghtbh = "";
    rs.executeQuery( "select zbbh,cslsh,zdlsh,dqlsh,csbbh,zdbbh,dqbbh from uf_lshgzb where lshlb=?",lshlb );
    if(rs.next()){
        String zbbh = Util.null2String(rs.getString( "zbbh" ));
        String cslsh = Util.null2String(rs.getString( "cslsh" ));
        String zdlsh = Util.null2String(rs.getString( "zdlsh" ));
        String dqlsh = Util.null2String(rs.getString( "dqlsh" ));
        String csbbh = Util.null2String(rs.getString( "csbbh" ));
        String zdbbh = Util.null2String(rs.getString( "zdbbh" ));
        String dqbbh = Util.null2String(rs.getString( "dqbbh" ));
        String lsh = autoGenericCode(dqlsh,4);
        if(fCompare(lsh,zdlsh)>0){
            lsh = autoGenericCode(cslsh,4);
        }
        String bbh = fAdd(dqbbh,"1");
        if(fCompare(dqbbh,zdbbh)>0){
            bbh = fAdd(csbbh,"1");
        }
        cghtbh = zbbh+"-"+cghtlx+lsh+"-"+bbh;
    }
    jsonData.put( "cghtbh", cghtbh );
    out.print( jsonData.toJSONString() );
%>
<%!
    private static String autoGenericCode(String code, int num) {
        String result = "";
        result = String.format("%0" + num + "d", Integer.parseInt(code) + 1);
        return result;
    }
    public static int fCompare(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        BigDecimal bg1 = new BigDecimal(a);
        BigDecimal bg2 = new BigDecimal(b);

        return bg1.compareTo(bg2);
    }
    public static String isZero(String a) {
        if ("".equals(a)) {
            a = "0";
        }
        return a;
    }
    public static String fAdd(String a, String b) {
        a = isZero(a);
        b = isZero(b);
        BigDecimal bg1 = new BigDecimal(a);
        BigDecimal bg2 = new BigDecimal(b);

        BigDecimal bd = bg1.add(bg2);

        return bd.toString();
    }
%>